package com.sl.au.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ContractInfoQuoteDevice;
import com.sl.au.repository.ContractInfoQuoteDeviceRepository;
import com.sl.au.service.ContractInfoQuoteDeviceService;

@Service("ContractInfoQuoteDeviceService")
@Transactional
public class ContractInfoQuoteDeviceServiceImpl extends BaseServiceImpl implements ContractInfoQuoteDeviceService{
	
	@Autowired
	ContractInfoQuoteDeviceRepository contractInfoQuoteDeviceRepository;
	
	@Override
	public ContractInfoQuoteDeviceRepository getRepository() {
		// TODO Auto-generated method stub
		return this.contractInfoQuoteDeviceRepository;
	}

	@Override
	public List<ContractInfoQuoteDevice> getContractInfos(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		// TODO Auto-generated method stub
		Page<ContractInfoQuoteDevice> pages = null;
		String dataFilter = condition.get("dataFilter");
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(dataFilter.equals("所有")){
			pages = contractInfoQuoteDeviceRepository.getContractInfos(condition.get("key"), pageable);
		}else if(dataFilter.equals("本部门")){
			pages = contractInfoQuoteDeviceRepository.getContractInfos(condition.get("key"), condition.get("superior"), pageable);
		}else if(dataFilter.equals("本片区")){
			pages = contractInfoQuoteDeviceRepository.getContractInfos(condition.get("key"), condition.get("superior"), condition.get("areaName"),  pageable);
		}else if(dataFilter.equals("本人")){
			pages = contractInfoQuoteDeviceRepository.getContractInfosByUser(condition.get("key"), condition.get("userId"), pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getContractInfosCount(Map<String, String> condition) {
		// TODO Auto-generated method stub
		String dataFilter = condition.get("dataFilter");
		if(dataFilter.equals("所有")){
			return this.contractInfoQuoteDeviceRepository.getContractInfoCount(condition.get("key"));
		}else if(dataFilter.equals("本部门")){
			return this.contractInfoQuoteDeviceRepository.getContractInfoCount(condition.get("key"), condition.get("superior"));
		}else if(dataFilter.equals("本片区")){
			return this.contractInfoQuoteDeviceRepository.getContractInfoCount(condition.get("key"), condition.get("superior"), condition.get("areaName"));
		}else{
			return this.contractInfoQuoteDeviceRepository.getContractInfoByUserCount(condition.get("key"), condition.get("userId"));
		}
	}

}
